home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_07 / allison / ddir1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-02  |  2.3 KB  |  96 lines

  1. LISTING 3 - Last month's "delete directory" program rewritten to
  2. throw C-string exceptions
  3.  
  4. // ddir1.cpp:  Remove subdirectory tree
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <io.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <dirent.h>
  11. #include <dir.h>
  12. #include <iostream.h>
  13. #include <fstream.h>
  14. #include <strstream.h>
  15. #include <except.h>
  16.  
  17. // Put response file in root directory
  18. char response_file[L_tmpnam+1] = "\\";
  19. // (Change this to "/" for UNIX)
  20.  
  21. main(int argc, char **argv)
  22. {
  23.     char *old_path = getcwd(NULL,MAXDIR);
  24.     void rd(char *);
  25.  
  26.     // Initialize response file for DOS "del" command
  27.     tmpnam(response_file+1);
  28.     ofstream(response_file) << "Y\n";
  29.  
  30.     // Delete the directories
  31.     while (--argc)
  32.     {
  33.         try
  34.         {
  35.             rd(*++argv);
  36.         }
  37.  
  38.         // Catch exceptions
  39.         catch(const char *x)
  40.         {
  41.             cerr << x << endl;
  42.         }
  43.     }
  44.  
  45.     // Clean-up
  46.     remove(response_file);
  47.     chdir(old_path);
  48.     return 0;
  49. }
  50.  
  51. void rd(char* dir)
  52. {
  53.     // Log onto the directory that is to be deleted
  54.     cout << strlwr(dir) << endl;
  55.     if (chdir(dir))
  56.         throw "Invalid directory";
  57.  
  58.     // Delete all normal files via OS shell
  59.     const size_t SH_CMD_LEN = 14;
  60.     char sh_cmd[L_tmpnam+SH_CMD_LEN+1];
  61.     ostrstream(sh_cmd,sizeof sh_cmd) << "del *.* <"
  62.                                      << response_file
  63.                                      << " >nul"
  64.                                      << ends;
  65.     system(sh_cmd);
  66.  
  67.     // Delete any remaining directory entries
  68.     DIR *dirp;
  69.     struct dirent *entry;
  70.     if ((dirp = opendir(".")) == NULL)
  71.         throw "Error opening directory";
  72.     while ((entry = readdir(dirp)) != NULL)
  73.     {
  74.         struct stat finfo;
  75.  
  76.         if (entry->d_name[0] == '.')
  77.             continue;
  78.         stat(entry->d_name,&finfo);
  79.         if (finfo.st_mode & S_IFDIR)
  80.             rd(entry->d_name);      // Subdirectory
  81.         else
  82.         {
  83.             // Enable delete of file, then do it
  84.             chmod(entry->d_name,S_IWRITE);
  85.             if (unlink(entry->d_name))
  86.                 throw "Error deleting file";
  87.         }
  88.     }
  89.     closedir(dirp);
  90.  
  91.     // Remove the directory from its parent
  92.     chdir("..");
  93.     if (rmdir(dir))
  94.         throw "Error deleting directory";
  95. }
  96.